package com.woniuxy.handler;

import com.alibaba.fastjson.JSON;
import com.woniuxy.service.GoodsService;
import com.woniuxy.service.GoodsShowService;
import com.work.framework.pojo.*;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author fengyao
 * @version 1.0
 * @date 2019/9/18 0018 下午 3:00
 */
@RequestMapping("/buyer")
@RestController
@EnableCaching
@Api(value = "商品上传",tags = "买家模块")
public class BuyerGoodsHandler {


    @Resource
    GoodsService goodsService;
    @Resource
    GoodsShowService goodsShowService;

    /**
     * 提供给auction-chat-provider-server服务
     * @param aid 通过前端传回aid
     * @return Auction信息
     */
    @GetMapping("/auction/{aid}")
    @ApiOperation("Auction返回给竞拍室")
    public Auction selectAuction(@PathVariable("aid") Integer aid){
        //select * from goods g,auction a where g.gid=#{gid} and a.valid=1 and a.gid=g.gid
        //1.aid访问goods--auction -->GoodsAuctionDTO
        Auction auction = goodsService.selectAuctionByGid(aid);
        System.out.println(auction);
        return auction;
    }


    @GetMapping("/login")
    @ApiOperation("Auction返回给竞拍室")
    public ResultDTO login(HttpSession session){
        Object user = session.getAttribute("user");
        if(user==null){
            return new ResultDTO(300,null,null);
        }else{
            return new ResultDTO(200,null,null);
        }
    }
    @GetMapping("/nologin")
    @ApiOperation("Auction返回给竞拍室")
    public void nologin(HttpSession session){
        session.removeAttribute("user");
    }

    /**
     * 提供给auction-chat-provider-server服务
     * @param aid 通过前端传回aid
     * @return Auction信息
     */
    @GetMapping("/selectGoodsAndAuctionByAid/{aid}")
    @ApiOperation("通过aid返回给uid")
    public GoodsAuctionDTO selectGoodsAndAuctionByAid(@PathVariable("aid") Integer aid){
        //select * from goods g,auction a where g.gid=#{gid} and a.valid=1 and a.gid=g.gid
        //1.aid访问goods--auction -->GoodsAuctionDTO
        GoodsAuctionDTO goodsAuctionDTO= goodsService.selectGoodsAuctionDTOByAid(aid);
        return goodsAuctionDTO;
    }

    /**
     * 商品详情实现
     * @param gid 前端传回gid数据
     * @return 返回一个GoodsAuctionRTO的集合类
     */
    @GetMapping("/goodsAndAuction/{gid}")
    @ApiOperation("商品详情页展示")
    @ApiParam(value = "商品id",name = "gid",type = "Integer")
    public GoodsAuctionDTO selectGoodsAndAuction(@PathVariable Integer gid){
        GoodsAuctionDTO goodsAuctionDTO = goodsService.selectGoodsAndAuctionByGid(gid);
        return goodsAuctionDTO;
    }


    @GetMapping("/myEnshrine/{pageNum}/{pageSize}")
    @ApiOperation("商品收藏展示")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页码",name = "pageNum",paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(value = "页面大小",name = "pageSize",paramType = "query",dataType = "Integer")
    })
    public ResultDTO selectMyEnshrine(@PathVariable Integer pageNum, @PathVariable Integer pageSize, HttpSession session){
//        Integer uid =Integer.getInteger(session.getAttribute("uid").toString());
        User user= (User) session.getAttribute("user");
        Integer uid=0;
        if(user!=null){
            uid=user.getUid();
        }else{
            return new ResultDTO(300,"用户未登陆",null);
        }
        List<GoodsAuctionDTO> goodsAuctionDTOS = goodsShowService.selectEnshrineGoods(pageNum, pageSize, uid);
        ResultDTO resultDTO = new ResultDTO();
        System.out.println("访问");
        resultDTO.setMessage("成功");
        resultDTO.setCode(200);
        resultDTO.setData(goodsAuctionDTOS);
        return resultDTO;
    }

    @PostMapping("/enshrine/{gid}")
    @ApiOperation("收藏商品")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品id",name = "gid",paramType = "query",dataType = "Integer"),
    })
    public ResultDTO updateEnshrineStatus(@PathVariable Integer gid, HttpSession session){
        User user= (User) session.getAttribute("user");
        Integer uid=0;
        if(user!=null){
            uid=user.getUid();
        }
        Enshrine enshrine = goodsService.selectEnshrine(uid, gid);
        if(enshrine!=null && enshrine.getFlage()==0){
            goodsService.updateEnshrine(uid,gid);
            goodsService.updateGoodsEnshrineCount(1,gid);
            return new ResultDTO(200,"成功收藏",null);
        }else if(enshrine==null){
            System.out.println("为null");
            goodsService.insertEnshrine(uid, gid);
            goodsService.updateGoodsEnshrineCount(1,gid);
            return new ResultDTO(200,"成功收藏",null);
        }else{
            return new ResultDTO(400,"已收藏，请勿重复收藏",null);
        }
    }

    @PostMapping("/noEnshrine/{gid}")
    @ApiOperation("取消商品收藏")
    public void noEnshrine(@PathVariable Integer gid){
        System.out.println("取消商品收藏");
        goodsService.noEnshrine(1,gid);
    }


    @GetMapping("/usersByGid/{gid}")
    @ApiOperation("获得某商品收藏用户信息")
    @ApiParam(value = "商品id",name = "gid",type = "Integer")
    public HashMap selectUsersByGid(@PathVariable Integer gid){
        GoodsAuctionDTO goodsAuctionDTO = goodsService.selectGoodsAndAuctionByGid(gid);
        //获得商品详情信息，用户列表
        List<User> users = goodsService.selectUsersByGid(gid);
        HashMap hashMap = new HashMap(3);
        return hashMap;
    }

    @GetMapping("/selectUsers/{gid}")
    @ApiOperation("获得某商品收藏用户信息")
    @ApiParam(value = "商品id",name = "gid",type = "Integer")
    public  List<User> selectUsers(@PathVariable Integer gid){
        List<User> users = goodsService.selectUsersByGid(gid);
        return users;
    }

    /**
     *
     * @param gid
     * @return
     */
    @GetMapping("/goodsInfo/{gid}")
    @ApiOperation("获得商品信息")
    @ApiImplicitParam(value = "商品id",name = "gid",type = "Integer")
    public GoodsAuctionDTO selectGoodsInfo(Integer gid){
        GoodsAuctionDTO goodsAuctionDTO = goodsService.selectGoodsAndAuctionByGid(gid);
        return goodsAuctionDTO;
    }

    @PostMapping("/updateStatus/{aid}/{status}")
    @ApiOperation("修改商品状态")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "竞拍id",name = "aid",type = "Integer"),
            @ApiImplicitParam(value = "竞拍状态",name = "status",type = "Integer")
    })
    public boolean updateStatus(@PathVariable Integer aid,@PathVariable Integer status){
        goodsService.updateAuctionStatus(aid,status);
        return true;
    }


}
